package com.hottv.meeting.videomeeting.module.meeting;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.meeting.bean.GetUserDetailRequest;
import com.hottv.meeting.videomeeting.module.meeting.data.MeetingData;
import com.hottv.meeting.videomeeting.module.white.WhiteAPI;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by chengwenqiang on 2019/6/5.
 */

public class MeetingAPI {

    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    public static String BMOP_KEY= "453b6fd3af81116543dbe6a3f00a1f01";

    private static final String bmop_host = "https://apiipvt.bmobpay.com/";

    public static final String BASE_URL_NEW = "http://120.236.210.166:8181/";
    public static final String GetDeviceByCode = BASE_URL_NEW + "app/devop/getDeviceByCode";

    private OkHttpClient client =null ;
    private Gson gson = null;

    private MeetingAPI()
    {
        client = new OkHttpClient();
        gson = new Gson();
    }

    private static MeetingAPI instance;

    public static  MeetingAPI getInstance()
    {
        if(instance==null)
        {
            instance = new MeetingAPI();
        }
        return   instance;
    }

    //请求会议
    public void openMeeting(Context context, MeetingData data, Callback callback)
    {
        String bodyStr = gson.toJson(data);
        Log.i(MeetingConfig.TAG,"body req"+bodyStr);
        RequestBody body = RequestBody.create(JSON, gson.toJson(data));
        Request request = new Request.Builder()
                .url(bmop_host + "1/applyMeeting")
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    //参加会议
    public void joinMeeting(Context context,String userId,String token,long timestamp,String meetingId,Callback callback)
    {

        Map<String, Object> roomSpec = new HashMap<>();
        roomSpec.put("userID", userId);
        roomSpec.put("token", token);
        roomSpec.put("timestamp", timestamp);
        roomSpec.put("meetingId", meetingId);
        String bodyStr = gson.toJson(roomSpec);
        RequestBody body = RequestBody.create(JSON, bodyStr);
        Log.i(MeetingConfig.TAG,"joinMeeting body:"+body.toString());
        Request request = new Request.Builder()
                .url(bmop_host + "1/joinmeeting")
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    //从服务器上获取主机名称
    public void getMeetingName(Context context,Callback callback)
    {
        GetUserDetailRequest  req = MeetUtils.getUserDetailRequest(context);
        String bodyStr = gson.toJson(req);
        RequestBody body = RequestBody.create(JSON, bodyStr);
        Log.i(MeetingConfig.TAG,"joinMeeting body:"+body.toString());
        Request request = new Request.Builder()
                .url(GetDeviceByCode)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);

    }



}
